package org.example.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

import org.example.pojo.Device;
import org.example.pojo.Result;

import org.example.utils.HttpUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test")
@Tag(name = "测试一下", description = "测试一下")
@Slf4j
public class TestController {
    @Value("${luqi.name}")
    String luqi;

    @PostMapping("/aaa")
    public Result aaa(@RequestBody Map map){
        
        try {
            String res = HttpUtil.doGet("http://127.0.0.1:8080/test/bbb");

//            Map<String, Object> params = new HashMap<>();
//            params.put("a", "luqi-a");
//            params.put("b", "luqi-b");
//            String res = HttpUtil.doPostForm("http://127.0.0.1:8080/test/testPostForm", params);


//            String res = HttpUtil.doPostJson("http://127.0.0.1:8080/test/testPostJson", map);

            ObjectMapper mapper = new ObjectMapper();
            Map ret = mapper.readValue(res, Map.class);
            return  Result.success(ret.get("data"));
        } catch (Exception e) {
            e.printStackTrace();
        }


        return  Result.success();

    }
    @GetMapping("/bbb")
    public Result bbb(){
        Map map = new HashMap();
        map.put("name", "sdfd");
        List<Map> list = new ArrayList<>();
        for(int i =0; i < 10; i++){
            Map<Object, Object> m = new HashMap<>();
            m.put("a", "aaa");
            m.put("bb", 100);
            list.add(m);
        }
        map.put("list", list);
        return Result.success(map);
    }

    @PostMapping("/testPostForm")
    @Operation(summary = "testPostForm", description = "testPostForm")
    public Result testPostForm(@Parameter(description="aaa") String a, @Parameter(description="bbb") String b){
        Map<String, Object> map = new HashMap<>();
        map.put("a", a);
        map.put("b", b);
        return Result.success(map);
    }

    @PostMapping("/testPostJson")
    @Operation(summary = "testPostJson", description = "testPostJson")
    public Result testPostJson(@RequestBody Map map){

        return Result.success(map);
    }
    @GetMapping("/testYml")
    @Operation(summary = "testYml", description = "testYml")
    public Result testYml(){
        return Result.success(luqi);
    }


}
